/**
 * @文件名称: TermQuartz.java
 * @版权声明 : Copyright (C) 2008-2016 北斗天汇（北京）科技有限公司
 * @创建人员: 徐斌
 * @文件描述:
 * @修改时间: 2017年6月16日 上午8:58:04
 * @修改历史： 2017年6月16日
 */

package com.bdth.controller;

import com.bdth.model.*;
import com.bdth.service.IRoleService;
import com.bdth.service.IUserService;
import com.bdth.utils.DateUtil;
import com.bdth.utils.HtmlUtil;
import com.bdth.utils.SessionUtils;
import com.bdth.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

	@Autowired
	private IUserService userService;

	@Autowired
	private IRoleService roleService;


/*	@Bean
	public TextMessageHandler textMessageHandler() {
		return new TextMessageHandler();
	}*/


	/**
	 * 用户首页
	 *
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request)
			throws Exception {
		return forword("/user/userList");
	}

	/**
	 * json 列表页面
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/dataList")
	public void dataList(PageModel pageModel, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		/*TextMessage message = new TextMessage("测试");
		textMessageHandler().sendMessageToUser("system", message);*/

		Map<String, Object> map = new HashMap<String, Object>();
		String userName = request.getParameter("userName");
		map.put("userName", userName);
		User user = SessionUtils.getUser(request);
		List<Integer> depIds = getDepIds(user.getDepId());
		map.put("depIds", depIds);
		map.put("startTime", request.getParameter("startTime"));
		map.put("endTime", request.getParameter("endTime"));
		//紧跟着的第一个查询方法会被分页
		PageHelper.startPage(pageModel.getPage(), pageModel.getRows());
		List<Map> dataList = userService.queryMapDataByMapList(map);
		//获得结果集条总数
		PageInfo pageList = new PageInfo(dataList);
		//设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", pageList.getTotal());
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}


	@RequestMapping("/getId")
	public void getId(Integer id, HttpServletResponse response)
			throws Exception {
		Map<String, Object> context = getRootMap();
		User bean = userService.queryById(id);
		if (bean == null) {
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}

	/*	//超级管理员
		if (bean.getUserFlag() == 0) {
			context.put("depId", 0);
			context.put("depName", "");
		} else {
			Department department = userService.queryDepByUserId(id);
			context.put("depId", department.getId());
			context.put("depName", department.getDepName());
		}*/

		context.put(SUCCESS, true);
		context.put("data", bean);

		HtmlUtil.writerJson(response, context);
	}


	/**
	 * 添加或修改数据
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public void save(User bean, HttpServletResponse response)
			throws Exception {
		Map<String, Object> context = new HashMap<String, Object>();

		if (StringUtil.isEmptyWithNullStr(bean.getLoginName())) {
			sendFailureMessage(response, "请输入登录名!");
			return;
		}
		User user = userService.queryUserByLoginName(bean.getLoginName().trim());
		if (bean.getRoleID() == null || bean.getRoleID().intValue() == 0) {
			sendFailureMessage(response, "请选择角色.");
			return;
		}
		if (bean.getDepId().intValue() == 0 || StringUtil.isEmptyWithNullStr(bean.getDepName())) {
			sendFailureMessage(response, "请选择所属分组!");
			return;
		}
		Role role = roleService.queryById(bean.getRoleID());
		if (role != null) {
			bean.setIdent(role.getRoleName());
		}
		bean.setLoginName(bean.getLoginName().trim());
		if (bean.getId() == null) {
			if (user != null) {
				sendFailureMessage(response, "登录名已存在.");
				return;
			}
			bean.setCreateTime(DateUtil.getPlusTime(new Date()));
			userService.add(bean);
		} else {
			if (user != null && user.getId().intValue() != bean.getId().intValue()) {
				sendFailureMessage(response, "登录名已存在.");
				return;
			}
/*			if (user != null && user.getUserFlag() != 0) {
				userService.update(bean, depId);
			} else {
				//超级管理员更新*/
			userService.updateBySelective(bean);
			//}

		}
		sendSuccessMessage(response, "保存成功~");
	}


	@RequestMapping("/delete")
	public void delete(Integer[] id, HttpServletResponse response)
			throws Exception {
		userService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}


	/**
	 * 假删除
	 *
	 * @param id
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/fakeDelete")
	public void fakeDelete(Integer[] id, HttpServletResponse response)
			throws Exception {

		for (int i : id) {

			User user = new User();
			user.setId(i);
			user.setDeleted(1);
			userService.deleteFake(user);
			sendSuccessMessage(response, "删除成功");
		}

	}


	/**
	 * 设置地图中心
	 *
	 * @return
	 */
	@RequestMapping("/setMapCenter")
	public void setMapCenter(String lng, String lat, HttpServletRequest request, HttpServletResponse response) throws Exception {
		User user = SessionUtils.getUser(request);
		User bean = new User();
		bean.setId(user.getId());
		bean.setLongitude(lng);
		bean.setLatitude(lat);
		userService.updateBySelective(bean);
		sendSuccessMessage(response, "保存成功~");

	}


}
